package org.whuims.pois.helper;

import org.whuims.pois.action.result.GraphData;
import org.whuims.pois.common.TimeUtils;

import java.util.*;

/**
 * Created by 声伟 on 2014/5/20 0020.
 */
public class ReportDataHelper {
    /**
     * 将每个系列的时间数据进行合并
     *
     * @param list
     * @return
     */
    public static List<Map<String, Long>> timedata(List<List<Calendar>> list) {
        List<Map<String, Long>> result = new ArrayList<Map<String, Long>>();
        if (list.size() == 0 || list.get(0).size() == 0) {
            return result;
        }
        //得到最大日期和最小日期，还有日期分组粒度
        Calendar max = list.get(0).get(0);
        Calendar min = list.get(0).get(list.get(0).size() - 1);
        for (int i = 1; i < list.size(); i++) {
            List<Calendar> timeData = list.get(i);
            if (timeData.size() > 0) {
                if (timeData.get(0).after(max)) {
                    max = timeData.get(0);
                }
                if (timeData.get(timeData.size() - 1).before(min)) {
                    min = timeData.get(timeData.size() - 1);
                }
            }
        }
        int day = TimeUtils.dValue(min, max);
        int groupGrain = 0;   //0:日，1：月，2：年
        if (day < 60) { //小于60天则按天展示
            groupGrain = 0;
        } else if (day < 365 * 2) {    //按月展示
            groupGrain = 1;
        } else { //按年展示
            groupGrain = 2;
        }
        //初始化结果map
        result = initMap(result, min, max, groupGrain, list);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Long> cl = result.get(i);
            List<Calendar> temp = list.get(i);
            for (int j = 0; j < temp.size(); j++) {
                Calendar tc = temp.get(j);
                String label = "";
                label = TimeUtils.getTimeLabel(groupGrain, tc);
                if (cl.containsKey(label)) {
                    cl.put(label, cl.get(label) + 1);
                } else {
                    cl.put(label, 1L);
                }
            }
        }
        return result;
    }


    public static List<Map<String, Long>> initMap(List<Map<String, Long>> result, Calendar min, Calendar max, int groupGrain, List<List<Calendar>> list) {
        result = new ArrayList<Map<String, Long>>();
        List<String> timeList = TimeUtils.iterateTimeList(min, max, groupGrain);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Long> map = new HashMap<String, Long>();
            for (int j = 0; j < timeList.size(); j++) {
                map.put(timeList.get(j), 0L);
            }
            result.add(map);
        }
        return result;
    }

    public static String jqPlotData(List<Map<String, Long>> result, boolean desc) {
        final boolean isDesc = desc;
        StringBuffer buf = new StringBuffer();
        buf.append("[");
        for (int i = 0; i < result.size(); i++) {
            buf.append("[");
            Map<String, Long> map = result.get(i);
            Iterator<Map.Entry<String, Long>> iterator = map.entrySet().iterator();
            List<Map.Entry<String, Long>> mapList = new ArrayList<Map.Entry<String, Long>>(map.entrySet());
            Collections.sort(mapList, new Comparator<Map.Entry<String, Long>>() {
                public int compare(Map.Entry<String, Long> mapping1, Map.Entry<String, Long> mapping2) {
                    if (isDesc) {
                        return mapping2.getKey().compareTo(mapping1.getKey());
                    } else {
                        return mapping1.getKey().compareTo(mapping2.getKey());

                    }
                }
            });
            int count = 0;

            for (int j = 0; j < mapList.size(); j++) {
                count++;
                Map.Entry<String, Long> entry = mapList.get(j);
                if (count > 1) {
                    buf.append(",");
                }
                buf.append("[\"" + entry.getKey() + "\"," + entry.getValue() + "]");
            }
            buf.append("]");
        }
        buf.append("]");
        return buf.toString();
    }

    public static GraphData graphData(List<Map<String, Integer>> result) {
        GraphData graphData = new GraphData();
        String[] label;
        if (result.size() == 0) {
            return graphData;
        }
        //得到label
        label = new String[result.get(0).size()];
        Iterator<String> iterator = result.get(0).keySet().iterator();
        int j = 0;
        while (iterator.hasNext()) {
            label[j++] = iterator.next();
        }
        //得到值
        List<int[]> data = new ArrayList<int[]>();
        for (int i = 0; i < result.size(); i++) {
            int[] d = new int[label.length];
            Map<String, Integer> map = result.get(i);
            for (int k = 0; k < label.length; k++) {
                d[k] = map.get(label[k]);
            }
            data.add(d);
        }
        graphData.setLabel(label);
        graphData.setData(data);
        return graphData;
    }

}
